import api from '../../../api/index'
Page({
  data: {
    result: null,
    resultindex: 0,
    list: [
      { name: '体操', image: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/sportsClass/newadd/ticao.png'},
      { name: '跑步', image: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/sportsClass/newadd/paobu.png'},
      { name: '跳绳', image: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/sportsClass/newadd/tiaosheng.png'}
    ],
    getdata: {}
  },
  onLoad(options) {
    wx.showLoading({
      title: '加载中...'
    })
    this.get()
    // const { once } = options
    // if (options.once) {
    //   this.setData({
    //     once
    //   });
    // }
  },
  async get() {
    try {
      const res = await api.post('/api/user/storage/get', {
        data: {
          'key': 'hxhealthsporttags'
        }
      })
      let array = res[0].value
      const list = this.data.list;
      console.log(array);
      if (array) {
        array = JSON.parse(array)
        this.setData({
          getdata: array
        });
        console.log(array, '----------------------');
        if (array.motionhabit) {
          // 有选择
          console.log('222');
          if (array.motionhabit) {
            let motionhabit = array.motionhabit
            console.log(motionhabit);
            const index = list.findIndex(item => item.name === motionhabit);
            console.log("result所在list数组的下标：", index);
            this.setData({
              resultindex: index,
              result: motionhabit
            });
          }
        } else {
          console.log('222221', this.data.list[0].name);
          let motionhabit = this.data.list[0].name
          this.setData({
            result: motionhabit
          });
        }
        wx.hideLoading()
      } else {
        let motionhabit = this.data.list[0].name
        this.setData({
          result: motionhabit
        });
        wx.hideLoading()
      }

    } catch (e) {
      console.log('get->', e)
      return null
    }
  },
  async putData() {
    let getdata = this.data.getdata
    console.log(getdata);
    console.log(this.data.result);
    // let lala = {"tags":"减脂减重,疾病康养","motionhabit":"直筒型","injuryillness":"膝盖","mtiontime":"30"}
    getdata.motionhabit = this.data.result
    console.log(getdata);
    try {
      const res = await api.post('/api/user/storage/set', {
        data: {
          'key': 'hxhealthsporttags',
          'value': JSON.stringify(getdata)
        }
      })
      console.log(res);
      if (res) {
        const pages = getCurrentPages();
        const pageStackLength = pages.length;
        console.log("页面栈数量：", pageStackLength);
        console.log('222111');
        wx.navigateTo({ url: '/sportsClass/pages/injuryillness/index' });
      }
    } catch (e) {
      console.log('put->', e)
      return null
    }
  },
  selectChange(e) { //滑动回调
    console.log(e);
    const index = e.currentTarget.dataset.index;
    this.setData({
      resultindex: index,
      result: this.data.list[index].name
    });
  }
})